/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.promo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.promo.PromoDistruk;
import utility.NameValueList;
import utility.StringUtils;
import utility.com.TextComponentUtils;
import utility.typeSafe.DiskonType;
import utility.typeSafe.EnumUtils;
import utility.typeSafe.HitungBonus;

/**
 *
 * @author jay
 */
public class PromoDistrukTM extends AbstractTableModel {
    
  //  private static final long serialVersionUID = 1L;\
    private List<NameValueList> diskonType = EnumUtils.enumToNameValueList(DiskonType.class);
    private List<NameValueList> caraHitung = EnumUtils.enumToNameValueList(HitungBonus.class);
  
    private List<PromoDistruk> data;

    public PromoDistrukTM(List<PromoDistruk> data) {
        this.data = data;
    }

    public PromoDistrukTM() {
        this(new ArrayList<PromoDistruk>(0));
    }

    public PromoDistruk get(int index) {
        return data.get(index);
    }

    public void add(PromoDistruk var) {
        data.add(var);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, PromoDistruk var) {
        data.set(index, var);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<PromoDistruk> vars) {
        data.addAll(vars);
        fireTableRowsInserted(getRowCount() - (vars.size()), getRowCount() - 1);
    }
         
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 10;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return (column==1 || column==2 || column==3 || column==4 || column==5 );
    }
    
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 1: 
                data.get(rowIndex).setDeskripsi((String)value);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
                
            case 5: 
                BigDecimal mrg = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setNilai(mrg);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
                
            case 4: 
                int angka = StringUtils.textToInt((String)value);
                data.get(rowIndex).setBatas(angka);
                break;                            
        }
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getDeskripsi();  
            case 2: if(data.get(rowIndex).getJenisDiskon() != null) {
                        int i = NameValueList.indexOf(diskonType, data.get(rowIndex).getJenisDiskon());
                        return diskonType.get(i).getValue();
                    } else {return null;}   
                
            case 3: if(data.get(rowIndex).getCaraHitung() != null) {
                        int i = NameValueList.indexOf(caraHitung, data.get(rowIndex).getCaraHitung());
                        return caraHitung.get(i).getValue();
                    } else {return null;}     
                        
            case 4: return data.get(rowIndex).getBatas();       
            case 5:  String displayMrg = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getNilai(), 2);
                return displayMrg; 
                
            case 6:  return data.get(rowIndex).getUpdated();     
//                            
            default: return null;
        }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "Keterangan";
            case 2: return "Jenis Diskon";
            case 3: return "Cara Hitung";
            case 4: return "Batas";
            case 5: return "Nilai";
            case 6: return "";
            case 7: return "";
            default: return null;
        }
    }

    
}
